<?php

/*
---------------------------------------------------------------------
- 功能描述: 框架基础类
- 文件名:Base.class.php
- 版本: 1.0
- 原作者:Robot
- 编写日期:2014/9/2
- 运行环境:---
---------------------------------------------------------------------
*/
class Base
{
	/**
	 * debug
	 *
	 * 调试模式，需要在url中传递 debug=on
	 * @var    boolean
	 * @access public
	 */
	static $debug = false;
	
	/**
	 * channel
	 *
	 *当前频道，对应controller、module下的文件夹
	 * @var    boolean
	 * @access public
	 */
	static $channel;
	
	/**
	 * controller
	 *
	 * @var    boolean
	 * @access public
	 */
	static $controller;
	
	/**
	 * action
	 *
	 * @var    boolean
	 * @access public
	 */
	static $action;
	
	/**
	 * mm
	 *
	 * @var    boolean
	 * @access public
	 */
	static $mm;

	/**
	 * 用户登录信息
	 *
	 * @var    boolean
	 * @access public
	 */
	static $user = array();
	
	/**
	 * 管理员登录信息
	 *
	 * @var    boolean
	 * @access public
	 */
	static $admin = array();

	/**
	 * cookie信息
	 *
	 * @var    boolean
	 * @access public
	 */
	static $cookies = array();
	
	/**
	 * get变量
	 *
	 * @access public
	 * @param
	 * @return void
	 */
	static $args = array();
	
	/**
	 * 构造函数
	 */
	function __construct(){}
	
	/**
	 * 调度方法
	 *
	 */
	function run()
	{
		//subsystem
		$host = $_SERVER['HTTP_HOST'];

		//数据接口
		if($host == DOMAIN_ADMIN)
		{
			$route_file = CONTROLLER_DIR . 'admin/indexRouter.php';
		}

		//主站
		else
		{
			$route_file = CONTROLLER_DIR . 'www/indexRouter.php';
		}

		if(!file_exists($route_file))
		{
			throw new MyException('The router [' . $route_file . "] not found.");
		}

		require_once $route_file; // 加载路由

		$router = new Router($aRule);

		/** 执行程序开始 **/
		$controller = self::$controller . 'Controller';
		$action = self::$action . 'Action';
		
		$controller_file = CONTROLLER_DIR . self::$channel . '/' . $controller . '.php';
		if(!file_exists($controller_file))
		{
			throw new MyException('The Controller [' . $controller_file . "] not found.");
		}
		
		require_once $controller_file; //加载controller文件
		
		$controller = new $controller(self::$channel);
		
		if(!method_exists($controller, $action))
		{
			throw new MyException('The Action [' . $action . '] not found.');
		}
		$controller->args = self::$args;
		$controller->$action();
		/** 执行程序结束 **/

		exit;
	}
	
	/**
	 * 脚本调度方法
	 *
	 */
	function runScript($argv)
	{
		//加载 登记的脚本
		require_once CONTROLLER_DIR . 'script/indexScript.php';

		//显示帮助信息
		if($argv[1] == '--help' || $non_existent = !in_array($argv[1], array_keys($aScript)))
		{
			//合法性检查
			if($non_existent)
			{
				$msg = "\n※※※ 参数错误或脚本名未登记。 ※※※\n\n";
			}
			$msg .= "--------------------------------------------------------\n";
			
			$msg .= "用法：/usr/local/php5.2/bin/php script.php 脚本名\n";
			$msg .= "\n可用脚本名:\n";
			
			foreach($aScript as $key => $val)
			{
				$msg .= "\t".$key."\t".$val."\n";	
			}

			$msg .= "\n说明：可用脚本名在/controller/script/indexScript.php中登记\n";
			$msg .= "--------------------------------------------------------\n";

			if(PHP_OS == "WINNT")
			{
				echo iconv("utf-8", "gb2312", $msg);
			}
			else
			{
				echo $msg;
			}
			exit;
		}

		/** 执行程序开始 **/
		$tmp = explode("_", $argv[1]);
		$group = array_shift($tmp);

		$controller = $group.'Controller';

		$action = "";
		foreach($tmp as $key => $val)
		{
			if($key == 0)
			{
				$action = $val;
			}
			else
			{
				$action .= ucfirst($val);
			}
		}
		$action .= 'Action';

		$controller_file = CONTROLLER_DIR . 'script/' . $controller . '.php';
		if(!file_exists($controller_file))
		{
			throw new MyException('The Controller [' . $controller_file . "] not found.");
		}
		
		require_once $controller_file; //加载controller文件
		
		$controller = new $controller(self::$channel);
		
		if(!method_exists($controller, $action))
		{
			throw new MyException('The Action [' . $action . '] not found.');
		}

		//参数
		self::$args = $argv;

		$controller->$action();
		/** 执行程序结束 **/

		exit;
	}

	/**
	 *
	 * 构造函数，初始化smarty和memcache
	 */
	function __get($var)
	{
		//模板
		if($var == 'smarty')
		{
			if(!$this->smarty)
			{
				include_once LIBRARY_DIR . '/Smarty/Smarty.class.php';
				$this->smarty = new Smarty();
				$this->smarty->setTemplateDir(TEMPLATE_DIR);
				$this->smarty->left_delimiter = '<%';
				$this->smarty->right_delimiter = '%>';
				$this->smarty->cache_dir = TEMPLATE_CACHE_DIR;
				$this->smarty->compile_dir = TEMPLATE_CACHE_DIR;
				$this->smarty->debugging = false;
				$this->smarty->assign('domain_url', DOMAIN_ROOT);
				$this->smarty->assign('static_url', STATIC_URL);
				spl_autoload_register ( '__autoload' );
			}
			
			if(DEBUG_ON)
			{
				if($_SERVER["REMOTE_ADDR"] == '127.0.0.1')
				{
					$this->smarty->assign("env", "[开发环境 - " . $_SERVER["REMOTE_ADDR"] . "]");
				}
				else
				{
					$this->smarty->assign("env", "[测试环境 - " . $_SERVER["REMOTE_ADDR"] . "]");
				}
			}
		}
		//模块
		else if(substr($var, -6, 6) == 'Module')
		{
			if(!$this->$var)
			{
				include_once LIBRARY_DIR . '/Core/InitDMO.class.php';
				include_once LIBRARY_DIR . '/Core/InitDTO.class.php';
				Common::load("PDOMySQL");
				$tmpVar = explode('_', $var);
				$module_file = MODULE_DIR . $tmpVar[0] . '/' . $tmpVar[1] . '.php';
				if(!file_exists($module_file))
				{
					if(DEBUG_ON)
					{
						throw new MyException('The Module ' . $module_file . ' not found.');
					}
					exit();
				}
				
				include_once $module_file;
				$this->$var = new $tmpVar[1]();
			}
		}
		//mem
		else if($var == 'mm')
		{
			ini_set('default_socket_timeout', -1);
			include CONFIG_DIR . 'mm.inc.php';
			Common::load("MyRedis");
			$this->mm = new MyRedis($aMemcacheServer, $aMemcacheQueueServer);
		}
		//分页
		else if($var == 'page')
		{
			Common::load('PageMaker');
			$this->$var = new PageMaker ();
		}
		
		return $this->$var;
	}
	
	/**
	 * 魔法函数，method不存在
	 *
	 * @access public
	 * @param  object
	 * @return void
	 */
	function __call($method, $parms)
	{
		throw new MyException("The " . get_class($this) . "->" . $method . '() does not exists.');
	}
	
	/**
	 * 成功提示方法
	 *
	 * @access public
	 * @param  object
	 * @return void
	 */
	public function success($sMessage, $sReturnUrl = "", $template = 'admin/success.html')
	{
		$sReturnUrl = $sReturnUrl ? $sReturnUrl : $_SERVER['HTTP_REFERER'];
		$this->smarty->assign("sMessage", $sMessage);
		$this->smarty->assign("sUrl", $sReturnUrl);
		$this->smarty->display($template);
		exit();
	}
	
	/**
	 * 错误处理方法
	 *
	 * @access public
	 * @param  object
	 * @return void
	 */
	public function error($sMessage, $sReturnUrl = "", $template = 'admin/error.html')
	{
		$sReturnUrl = $sReturnUrl ? $sReturnUrl : $_SERVER['HTTP_REFERER'];
		$this->smarty->assign("sMessage", $sMessage);
		$this->smarty->assign("sUrl", $sReturnUrl);
		$this->smarty->display($template);
		exit();
	}
}

?>
